<?php

/**
 *
 * 微信公众平台
 * @author Njs hxtgirq710@qq.com
 *
 */
define('TOKEN','Sydney');
class WeiXinAction extends Action {

    function index() {
        self::api();
    }
    
    function api() {
        if(isset($_GET['echostr']))
            self::valid();
        else
            self::reply();        
    }
    
    
    public function reply() {
		$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
		if (!empty($postStr)) {
              	$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
                $fromUsername = (string)$postObj->FromUserName;
                $toUsername = (string)$postObj->ToUserName;
                $keyword = trim($postObj->Content);
                $time = time();

                $this->display(self::getTpl());

        }else {
        	echo "非法访问";
        	exit;
        }
    }

	public function getMsg($info){
		
	}

	public function getTpl($info){
	    $tpl = 'text';
        return $tpl;
	}
    
    public function valid() {
        $echoStr = $_GET["echostr"];
        if($this->checkSignature()) {
        	echo $echoStr;
        	exit;
        }
    }
		
	private function checkSignature() {
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];
		$token = TOKEN;
		$tmpArr = array($token, $timestamp, $nonce);
		sort($tmpArr);
		$tmpStr = implode( $tmpArr );
		$tmpStr = sha1( $tmpStr );
		
		if ($tmpStr == $signature) {
			return true;
		} else {
			return false;
		}
	}
}